﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace QuanLyVatLieuXayDung
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            Global.Plugins.FindPlugins(Application.StartupPath + @"\Plugins");
            timer1.Start();
            //Add each plugin to the treeview
            string phanquyen = "admin";
            foreach (Types.AvailablePlugin pluginOn in Global.Plugins.AvailablePlugins)
            {
                if (pluginOn.Instance.Name != "Trang Chủ" && pluginOn.Instance.Name != "Quản Trị")
                {
                    TreeNode newNode = new TreeNode(pluginOn.Instance.Name);
                    imageList1.Images.Add(Image.FromFile(pluginOn.Instance.FileIcon));
                    newNode.ImageIndex = 0;
                    this.treeView1.Nodes.Add(newNode);
                    newNode = null;
                }
                if (pluginOn.Instance.Name == "Quản Trị")
                {
                    button2.Text = pluginOn.Instance.Name;
                }
                if (pluginOn.Instance.Name == "Trang Chủ")
                {
                    button3.Text = pluginOn.Instance.Name;
                    Types.AvailablePlugin selectedPlugin = Global.Plugins.AvailablePlugins.Find(pluginOn.Instance.Name);

                    if (selectedPlugin != null)
                    {
                        //Again, if the plugin is found, do some work...


                        //This part adds the plugin's info to the 'Plugin Information:' Frame
                        //this.lblPluginName.Text = selectedPlugin.Instance.Name;
                        //this.lblPluginVersion.Text = "(" + selectedPlugin.Instance.Version + ")";
                        //this.lblPluginAuthor.Text = "By: " + selectedPlugin.Instance.Author;
                        //this.lblPluginDesc.Text = selectedPlugin.Instance.Description;

                        //Clear the current panel of any other plugin controls... 
                        //Note: this only affects visuals.. doesn't close the instance of the plugin
                        this.panel1.Controls.Clear();

                        //Set the dockstyle of the plugin to fill, to fill up the space provided
                        selectedPlugin.Instance.MainInterface.Dock = DockStyle.Fill;
                        //Finally, add the usercontrol to the panel... Tadah!
                        this.panel1.Controls.Add(selectedPlugin.Instance.MainInterface);

                    }
                }
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (this.treeView1.SelectedNode != null)
            {
                //Get the selected Plugin
                Types.AvailablePlugin selectedPlugin = Global.Plugins.AvailablePlugins.Find(treeView1.SelectedNode.Text.ToString());

                if (selectedPlugin != null)
                {
                    //Again, if the plugin is found, do some work...


                    //This part adds the plugin's info to the 'Plugin Information:' Frame
                    //this.lblPluginName.Text = selectedPlugin.Instance.Name;
                    //this.lblPluginVersion.Text = "(" + selectedPlugin.Instance.Version + ")";
                    //this.lblPluginAuthor.Text = "By: " + selectedPlugin.Instance.Author;
                    //this.lblPluginDesc.Text = selectedPlugin.Instance.Description;

                    //Clear the current panel of any other plugin controls... 
                    //Note: this only affects visuals.. doesn't close the instance of the plugin
                    this.panel1.Controls.Clear();

                    //Set the dockstyle of the plugin to fill, to fill up the space provided
                    selectedPlugin.Instance.MainInterface.Dock = DockStyle.Fill;

                    //Finally, add the usercontrol to the panel... Tadah!
                    this.panel1.Controls.Add(selectedPlugin.Instance.MainInterface);

                }

            }
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                
                Global.Plugins.ClosePlugins();
                Program.thdangNhap.Start();
                this.Close();
            }
            catch (Exception ex)
            { }
        }

        private void zButton1_zClick(object Sender, MouseEventArgs e)
        {
            //zButton1
        }
        bool ktAnHienPanleRight = false;
        private void button1_Click(object sender, EventArgs e)
        {
            if (ktAnHienPanleRight)
            {
                btnAnHien.Image = Properties.Resources.Actions_go_first_icon;
                panlRitght.Show();
                ktAnHienPanleRight = false;
            }
            else
            {
                btnAnHien.Image = Properties.Resources.Actions_go_last_icon;
                ktAnHienPanleRight = true;
                panlRitght.Hide();
            }
        }

        private void button1_MouseHover(object sender, EventArgs e)
        {
            button1.Image = Properties.Resources.Administrative_Tools_iconbu;
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            button1.Image = Properties.Resources.Administrative_Tools_icon;
        }

        private void button2_MouseHover(object sender, EventArgs e)
        {
            button2.Image = Properties.Resources.License_manager_iconbu;
        }

        private void button2_MouseLeave(object sender, EventArgs e)
        {
            button2.Image = Properties.Resources.License_manager_icon;
        }

        private void button3_MouseHover(object sender, EventArgs e)
        {
            button3.Image = Properties.Resources.User_Files_icon;
        }

        private void button3_MouseLeave(object sender, EventArgs e)
        {
            button3.Image = Properties.Resources.User_Files_icon1;
        }
        bool anLen = true;
        private void button4_Click(object sender, EventArgs e)
        {
            if (anLen)
            {
                panel2.Hide();
                anLen = false;
                button4.Image = Properties.Resources.Misc_Options_icon1;
            }
            else
            {
                panel2.Show();
                anLen = true;
                button4.Image = Properties.Resources.Misc_Options_icon;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = DateTime.Now.ToShortTimeString() + "  " + DateTime.Now.Day + "/" + DateTime.Now.Month+ "/" + DateTime.Now.Year;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Global.Plugins.ClosePlugins();
            Program.thdangNhap = new Thread(new ThreadStart(Program.openformDangNhap));
            Program.thdangNhap.Start();
            this.Close();
        }

        private void textBox1_MouseDown(object sender, MouseEventArgs e)
        {
            textBox1.Text = "";
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            FormDoiMatKhau frm = new FormDoiMatKhau();
            frm.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Types.AvailablePlugin selectedPlugin = Global.Plugins.AvailablePlugins.Find(button3.Text);

            if (selectedPlugin != null)
            {
                //Again, if the plugin is found, do some work...


                //This part adds the plugin's info to the 'Plugin Information:' Frame
                //this.lblPluginName.Text = selectedPlugin.Instance.Name;
                //this.lblPluginVersion.Text = "(" + selectedPlugin.Instance.Version + ")";
                //this.lblPluginAuthor.Text = "By: " + selectedPlugin.Instance.Author;
                //this.lblPluginDesc.Text = selectedPlugin.Instance.Description;

                //Clear the current panel of any other plugin controls... 
                //Note: this only affects visuals.. doesn't close the instance of the plugin
                this.panel1.Controls.Clear();

                //Set the dockstyle of the plugin to fill, to fill up the space provided
                selectedPlugin.Instance.MainInterface.Dock = DockStyle.Fill;

                //Finally, add the usercontrol to the panel... Tadah!
                this.panel1.Controls.Add(selectedPlugin.Instance.MainInterface);

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Types.AvailablePlugin selectedPlugin = Global.Plugins.AvailablePlugins.Find(button2.Text);

            if (selectedPlugin != null)
            {
                //Again, if the plugin is found, do some work...


                //This part adds the plugin's info to the 'Plugin Information:' Frame
                //this.lblPluginName.Text = selectedPlugin.Instance.Name;
                //this.lblPluginVersion.Text = "(" + selectedPlugin.Instance.Version + ")";
                //this.lblPluginAuthor.Text = "By: " + selectedPlugin.Instance.Author;
                //this.lblPluginDesc.Text = selectedPlugin.Instance.Description;

                //Clear the current panel of any other plugin controls... 
                //Note: this only affects visuals.. doesn't close the instance of the plugin
                this.panel1.Controls.Clear();

                //Set the dockstyle of the plugin to fill, to fill up the space provided
                selectedPlugin.Instance.MainInterface.Dock = DockStyle.Fill;

                //Finally, add the usercontrol to the panel... Tadah!
                this.panel1.Controls.Add(selectedPlugin.Instance.MainInterface);

            }
        }
    }
}
